'use strict';
const iconv = require('iconv-lite');

async function zhihu() {
	// 知乎热榜
	var url = 'https://www.zhihu.com/api/v3/feed/topstory/hot-lists/total?limit=50'
	// var url = 'https://www.zhihu.com/question/问题id'
	var res = await uniCloud.httpclient.request(url, {
		method: 'GET',
		dataType: 'json'
	})
	var result = [];
	res.data.data.forEach(function(value) {
		let obj = {
			url: 'https://www.zhihu.com/question/' + value.target.id,
			title: value.target.title,
			hot_value: value.detail_text
		}
		result.push(obj)
	})

	//返回数据给客户端
	return result
}
async function douyin() {
	// 抖音热榜
	// var url = 'https://aweme.snssdk.com/aweme/v1/hot/search/list/'
	// var url = 'https://www.iesdouyin.com/web/api/v2/hotsearch/billboard/aweme/'
	var url = 'https://www.iesdouyin.com/web/api/v2/hotsearch/billboard/word/'

	var res = await uniCloud.httpclient.request(url, {
		method: 'GET',
		dataType: 'json'
	})
	var result = [];
	res.data.word_list.forEach(function(value) {
		let obj = {
			url: "snssdk1128://search/trending",
			title: value.word,
			hot_value: value.hot_value
		}
		result.push(obj)
	})

	//返回数据给客户端
	return result
}
async function video() {
	// 抖音视频
	// var url = 'https://aweme.snssdk.com/aweme/v1/hot/search/list/'
	var url = 'https://www.iesdouyin.com/web/api/v2/hotsearch/billboard/aweme/'
	// var url = 'https://www.iesdouyin.com/web/api/v2/hotsearch/billboard/word/'

	var res = await uniCloud.httpclient.request(url, {
		method: 'GET',
		dataType: 'json'
	})
	var result = [];
	res.data.aweme_list.forEach(function(value) {
		let obj = {
			url: value.aweme_info.share_url,
			title: value.aweme_info.desc,
			hot_value: value.hot_value
		}
		result.push(obj)
	})

	//返回数据给客户端
	return result
}
async function weibo() {

	// 微博热搜
	var url = 'https://s.weibo.com/top/summary'
	// var url = 'https://s.weibo.com/weibo?q=搜索词&Refer=top'
	var res = await uniCloud.httpclient.request(url, {
		method: 'GET',
		dataType: 'text'
	})
	var patt =
		/<td class="td-01 ranktop">\d+<\/td>\s+<td class="td-02">\s+<a href=".*?" target="_blank">(.*?)<\/a>\s+<span>(\d+)<\/span>\s+(<img.*?\/>)?\s+<\/td>\s+<td class="td-03">(<i class="icon-txt icon-txt-.*?">(.)<\/i>)?<\/td>/g
	var arr, result = [];
	while ((arr = patt.exec(res.data)) != null) {
		// console.log(arr[1], arr[2], arr[5])
		let obj = {
			url: 'https://s.weibo.com/weibo?q=' + encodeURIComponent(arr[1]) + '&Refer=top',
			title: arr[1],
			hot_value: arr[2],
			tag: arr[5]
		}
		result.push(obj)
	}

	//返回数据给客户端
	return result
}
async function weixin() {

	// 微信搜狗
	var url = 'https://weixin.sogou.com'
	// var url = 'https://weixin.sogou.com/weixin?type=2&ie=utf8&s_from=hotnews&query=搜索词'
	var res = await uniCloud.httpclient.request(url, {
		method: 'GET',
		dataType: 'text'
	})
	var patt =
		/">(.*?)<\/a>\s+<span class="lan-line"><span style="width:(\d+)%"><\/span><\/span>/g
	var arr, result = [];
	while ((arr = patt.exec(res.data)) != null) {
		// console.log(arr[1], arr[2], arr[5])
		let obj = {
			url: 'https://weixin.sogou.com/weixin?type=2&ie=utf8&s_from=hotnews&query=' + encodeURIComponent(arr[1]),
			title: arr[1],
			hot_value: arr[2]
		}
		result.push(obj)
	}

	//返回数据给客户端
	return result
}
async function baidu() {

	// 百度风云榜
	var url = 'http://top.baidu.com/buzz?b=1'
	// var url = 'https://www.baidu.com/s?wd=搜索词'
	var res = await uniCloud.httpclient.request(url, {
		method: 'GET'
	})
	var patt =
		/<a class="list-title" target="_blank" href=.*?>(.*?)<\/a>[\s\S]*?<span class="icon-.+?">(\d+)<\/span>/g
	var arr, result = [],
		html = iconv.decode(res.data, 'gbk');
	while ((arr = patt.exec(html)) != null) {
		let obj = {
			url: 'https://www.baidu.com/s?wd=' + encodeURIComponent(arr[1]),
			title: arr[1],
			hot_value: arr[2]
		}
		result.push(obj)
	}

	//返回数据给客户端
	return result
}
exports.main = async (event, context) => {
	//event为客户端上传的参数
	// event.type
	// weibo zhihu douyin video baidu 
	console.log('event : ', event)
	return eval(event.type + '()')

};
